#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

// 十进制转二进制
vector<int> decimalToBinary(int decimal) {
    vector<int> binary;
    while (decimal > 0) {
        binary.push_back(decimal % 2);
        decimal /= 2;
    }
    reverse(binary.begin(), binary.end());
    return binary;
}

// 二进制转十进制
int binaryToDecimal(vector<int> binary) {
    int decimal = 0;
    for (int i = 0; i < binary.size(); i++) {
        decimal += binary[i] * pow(2, binary.size() - 1 - i);
    }
    return decimal;
}

// 十进制转十六进制
string decimalToHex(int decimal) {
    string hex = "0123456789ABCDEF";
    string result = "";
    while (decimal > 0) {
        int remainder = decimal % 16;
        result = hex[remainder] + result;
        decimal /= 16;
    }
    return result;
}

// 十六进制转十进制
int hexToDecimal(string hex) {
    string hexTable = "0123456789ABCDEF";
    int decimal = 0;
    for (int i = hex.length() - 1; i >= 0; i--) {
        int index = hexTable.find(hex[i]);
        if (index == -1) {
            return -1;
        }
        decimal += index * pow(16, hex.length() - 1 - i);
    }
    return decimal;
}

// 十进制转八进制
vector<int> decimalToOctal(int decimal) {
    vector<int> octal;
    while (decimal > 0) {
        octal.push_back(decimal % 8);
        decimal /= 8;
    }
    reverse(octal.begin(), octal.end());
    return octal;
}

// 八进制转十进制
int octalToDecimal(vector<int> octal) {
    int decimal = 0;
    for (int i = 0; i < octal.size(); i++) {
        decimal += octal[i] * pow(8, octal.size() - 1 - i);
    }
    return decimal;
}

// 十进制转任意十以下的进制(十六进制除外因为有字母)
vector<int> decimalToAny(int decimal, int base) {
    if (base < 2 || base > 16) {
        return vector<int>();
    }
    vector<int> any;
    while (decimal > 0) {
        any.push_back(decimal % base);
        decimal /= base;
    }
    reverse(any.begin(), any.end());
    return any;
}

// 任何十以下的进制转十进制(十六进制除外因为有字母)
int anyToDecimal(string any, int base) {
    if (base < 2 || base > 16) {
        return -1;
    }
    string hexTable = "0123456789ABCDEF";
    int decimal = 0;
    for (int i = any.length() - 1; i >= 0; i--) {
        int index = hexTable.find(any[i]);
        if (index == -1) {
            return -1;
        }
        decimal += index * pow(base, any.length() - 1 - i);
    }
    return decimal;
}

int main() {
    int decimal = 50;
    vector<int> binary = decimalToBinary(decimal);
    cout << "Binary: ";
    for (int i = 0; i < binary.size(); i++) {
        cout << binary[i];
    }
    cout << endl;

    vector<int> octal = decimalToOctal(decimal);
    cout << "Octal: ";
    for (int i = 0; i < octal.size(); i++) {
        cout << octal[i];
    }
    cout << endl;

    string hex = decimalToHex(decimal);
    cout << "Hex: " << hex << endl;

    vector<int> any = decimalToAny(decimal, 3);
    cout << "Any: ";
    for (int i = 0; i < any.size(); i++) {
        cout << any[i];
    }
    cout << endl;

    string anyStr = "1201";
    int decimal2 = anyToDecimal(anyStr, 3);
    cout << "Decimal: " << decimal2 << endl;
    cout << endl;

    return 0;
}